import { Controller, Get, Post, Body, Patch, Param, Delete, Query, ParseIntPipe } from '@nestjs/common';
import { SelectCourseService } from './select_course.service';
import { CreateSelectCourseDto } from './dto/create-select-course.dto';
import { UpdateScoreDto } from './dto/update-score.dto';
import { QuerySelectCourseDto } from './dto/query-select-course.dto';
import { ApiOperation, ApiResponse } from '@nestjs/swagger';
// import { BatchUpdateScoreDto } from './dto/batch-update-score.dto';

@Controller('select_course')
export class SelectCourseController {
  constructor(private readonly selectCourseService: SelectCourseService) {}

  @Post()
  @ApiOperation({ summary: '学生选课' })
  @ApiResponse({ status: 201, description: '选课成功' })
  create(@Body() createSelectCourseDto: CreateSelectCourseDto) {
    return this.selectCourseService.create(createSelectCourseDto);
  }

  @Post(':id/drop')
  @ApiOperation({ summary: '学生退课' })
  @ApiResponse({ status: 200, description: '退课成功' })
  dropCourse(@Param('id', ParseIntPipe) id: number) {
    return this.selectCourseService.dropCourse(id);
  }

  @Patch(':id/score')
  @ApiOperation({ summary: '教师录入成绩' })
  @ApiResponse({ status: 200, description: '录入成功' })
  updateScore(
    @Param('id', ParseIntPipe) id: number,
    @Body() updateScoreDto: UpdateScoreDto
  ) {
    return this.selectCourseService.updateScore(id, updateScoreDto);
  }

  @Get()
  @ApiOperation({ summary: '查询选课记录' })
  @ApiResponse({ status: 200, description: '查询成功' })
  findAll(@Query() query: QuerySelectCourseDto) {
    return this.selectCourseService.findAll(query);
  }

  @Get('student/:sno')
  @ApiOperation({ summary: '获取学生选课列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  findByStudent(
    @Param('sno') sno: string,
    @Query('semester_id') semester_id?: string
  ) {
    return this.selectCourseService.findByStudent(sno, semester_id ? +semester_id : undefined);
  }

  @Get('teacher/:tno')
  @ApiOperation({ summary: '获取教师课程学生列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  findByTeacher(
    @Param('tno', ParseIntPipe) tno: number,
    @Query('semester_id') semester_id?: string,
    @Query('cno') cno?: string,
    @Query('page') page?: string,
    @Query('pageSize') pageSize?: string
  ) {
    const pageNum = page ? parseInt(page, 10) : 1;
    const pageSizeNum = pageSize ? parseInt(pageSize, 10) : 10;
    return this.selectCourseService.findByTeacher(
      tno, 
      semester_id ? +semester_id : undefined, 
      cno ? +cno : undefined,
      pageNum, 
      pageSizeNum
    );
  }

  @Get('teacher/:tno/courses')
  @ApiOperation({ summary: '获取教师开课列表（用于成绩管理）' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getTeacherCourses(
    @Param('tno', ParseIntPipe) tno: number,
    @Query('semester_id') semester_id?: string
  ) {
    return this.selectCourseService.getTeacherCourses(tno, semester_id ? +semester_id : undefined);
  }

  @Get('course/:offering_id/students')
  @ApiOperation({ summary: '获取课程学生成绩列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getCourseStudents(
    @Param('offering_id', ParseIntPipe) offering_id: number,
    @Query('teacher_tno', ParseIntPipe) teacher_tno: number
  ) {
    return this.selectCourseService.getCourseStudentScores(offering_id, teacher_tno);
  }

  // @Patch('batch-score')
  // @ApiOperation({ summary: '批量录入成绩' })
  // @ApiResponse({ status: 200, description: '录入成功' })
  // async batchUpdateScores(
  //   @Body() batchUpdateDto: BatchUpdateScoreDto,
  //   @Query('teacher_tno', ParseIntPipe) teacher_tno: number
  // ) {
  //   return this.selectCourseService.batchUpdateScores(batchUpdateDto, teacher_tno);
  // }
}
